function R=save3DHFilter(filename,n,precond,precision,H)
    global verbose
    timestamp=now;
    if verbose
        fprintf('creating 3D filter on %s with n = %d and precond %d for future usage\n',datestr(timestamp),n,precond);
    end
    if nargin<5
        H=calculatePreconditioner3D(n,precond,precision);
    end
    Htmp=H.filter(1:n,1:n,1:n);
    fid=fopen(filename,'w');
    if fid<0
        error('failed to open file %s for writing, filter was not created\n',filename);
    else
        fwrite(fid,n,'int');
        fwrite(fid,timestamp,'real*8');
        fwrite(fid,precond,'int');
        for i=1:n
            for j=0:i-1
                for k=0:j
                    if strcmp(precision,'single')
                        fwrite(fid,real(Htmp(i+j*n+k*n^2)),'real*4');
                    elseif strcmp(precision,'double')
                        fwrite(fid,real(Htmp(i+j*n+k*n^2)),'real*8');
                    else
                        error('precision must be single or double');
                    end
                end
            end
        end
        fclose(fid);
    end
    R=H;
end